<template>
  <div class="app-container" style="height:100%;">
    <!-- <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px" class="bg-white p-1">
      <el-form-item label="字典名称" prop="dictName">
        <el-input
            v-model="queryParams.query.params.dictName"
            placeholder="请输入字典名称"
            clearable
            size="small"
            style="width: 240px"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="字典类型" prop="dictType">
        <el-input
            v-model="queryParams.query.params.dictType"
            placeholder="请输入字典类型"
            clearable
            size="small"
            style="width: 240px"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8 my-1">
      <el-col :span="1.5">
        <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['system:dict:add']"
        >新增
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="searchLoading" :data="typeList" @selection-change="handleSelectionChange" :header-cell-style="{background: '#DDD'}">
      <el-table-column label="字典名称" align="center" prop="dictName" :show-overflow-tooltip="true"/>
      <el-table-column label="字典类型" align="center" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <el-button type="text" @click="toDictData(scope.row.dictType)">{{ scope.row.dictType }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:dict:edit']"
          >修改
          </el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:dict:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    /> -->


    <myTableSearch
      ref="myTableSearchRef"
      :isOperate="true"
      :operateWidth="120"
      :titleName="$route.meta.title"
      :queryFn="getList"
      :formData="formData"
      :tableLabel="tableLabel"
      @recombinationData="recombinationData"
    >
      <template slot="tool">
        <div class="flex flex-x">
          <div class="fillBtn" @click="handleAdd" v-hasPermi="['system:user:add']">
            <img src="@/assets/images/gzq/icon-add.png" alt="" />
            <span>新增数据</span>
          </div>
        </div>
      </template>
      <template #createTime="scope">
        <span>{{ parseTime(scope.row.createTime) }}</span>
      </template>
      <template #dictType="scope">
        <span class="cursor" style="color:#30AAD1;border-bottom:1px solid #30AAD1" @click="toDictData(scope.row.dictType)">{{ scope.row.dictType }}</span>
      </template>
      <template slot="operate" slot-scope="scope">
        <div class="operateBtn flex">
          <div class="itemBtn" @click="handleUpdate(scope.row)" v-hasPermi="['system:dict:edit']">修改</div>
          <div class="itemBtn ml-1" @click="handleDelete(scope.row)" v-hasPermi="['system:dict:remove']">删除</div>
        </div>
      </template>
    </myTableSearch>

    <!-- 添加或修改参数配置对话框 -->
    <myDialog :isDialog="open" :title="title" :width="'500px'" @handleClose="cancel">
      <template slot="content">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="字典名称" prop="dictName">
            <el-input v-model="form.dictName" placeholder="请输入字典名称"/>
          </el-form-item>
          <el-form-item label="字典类型" prop="dictType">
            <el-input v-model="form.dictType" placeholder="请输入字典类型"/>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
          </el-form-item>
        </el-form>
      </template>
      <template slot="footer">
        <div class="formBtnBox">
          <div class="saveBtn" @click="submitForm">确 定</div>
          <div class="editBtn ml-4"  @click="cancel">取 消</div>
        </div>
      </template>
    </myDialog>
  </div>
</template>

<script>
import {typePage, typeDetail, typeAdd, typeUpdate, typeDeleteForce} from "@/api/sys/dictType";

export default {
  name: "DictType",
  data() {
    return {
      formData: [
        {
          label: "字典名称",
          type: "input",
          key: "dictName",
        },
        {
          label: "字典类型",
          type: "input",
          key: "dictType",
        },
      ],
      tableLabel: [
        {
          prop: "dictName",
          label: "字典名称",
        },
        {
          prop: "dictType",
          label: "字典类型",
        },
        {
          prop: "remark",
          label: "备注",
        },
        {
          prop: "createTime",
          label: "创建时间",
        },
      ],
      searchLoading: true,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 选中数组
      selectedIds: [],
      typeList: [],
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      form:{},
      rules:{
        dictName: [
          { required: true, message: "字典名称不能为空", trigger: "blur" }
        ],
        dictType: [
          { required: true, message: "字典类型不能为空", trigger: "blur" }
        ]
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        query: {
          params:{},

        }
      },
    }
  },
  created() {
    // this.getList();
  },
  methods: {
    getList(e) {
      return typePage(e)
    },
    //格式化搜索字段
    recombinationData(param, callback) {
      param = {
        ...param,
      };
      callback({ ...param });
    },
    toDictData(dickType) {
      this.$router.push({
        // path: "/sys/dict/data",
         path: "/sys/dictData",
        query: {
          dickType: dickType
        }
      })
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        query:{
          params:{
            dictName: undefined,
            dictType: undefined
          }
        }
      }
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加字典类型";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selectedIds = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      typeDetail(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改字典类型";
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除该数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return typeDeleteForce(row.id);
      }).then(() => {
        // this.getList();
        this.$refs.myTableSearchRef.searchReset();
        this.msgSuccess("删除成功");
      })
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id !== undefined) {
            typeUpdate(this.form).then(() => {
              this.msgSuccess("修改成功");
              this.open = false;
              // this.getList();
              this.$refs.myTableSearchRef.searchReset();
            });
          } else {
            typeAdd(this.form).then(() => {
              this.msgSuccess("新增成功");
              this.open = false;
              // this.getList();
              this.$refs.myTableSearchRef.searchReset();
            });
          }
        }
      });
    },
    /** 清理缓存按钮操作 */
    handleClearCache() {

    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        dictName: undefined,
        dictType: undefined,
        remark: undefined
      };
      this.resetForm("form");
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
  }
}
</script>

<style scoped>

</style>
